﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Assist;
using YArchitech.Controls;
using YArchitech.LIB;

namespace HYRElectric.WirePlace
{
	public partial class DlgSetRelationMap : System.Windows.Forms.Form
	{
		public DlgSetRelationMap()
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.btnAdd.Image = Resource.GetImage("AddNew16");
			this.btnDel.Image = Resource.GetImage("delete16");
			this.toolTip.SetToolTip(this.btnAdd, "添加");
			this.toolTip.SetToolTip(this.btnDel, "删除");
			this.InitGridViewData();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (!this.CheckCellValues())
			{
				return;
			}
			this.SaveXml();
			base.DialogResult = DialogResult.OK;
		}

		private void button2_Click(object sender, EventArgs e)
		{
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.dataGridView.SelectedRows.Count < 1)
			{
				return;
			}
			int index = this.dataGridView.SelectedRows[0].Index;
			this.dataGridView.Rows.Insert(index, 1);
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			foreach (object obj in this.dataGridView.SelectedRows)
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (!dataGridViewRow.IsNewRow)
				{
					this.dataGridView.Rows.Remove(dataGridViewRow);
				}
			}
		}

		private bool CheckCellValues()
		{
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (!dataGridViewRow.IsNewRow)
				{
					if (dataGridViewRow.Cells[1].Value == null || dataGridViewRow.Cells[0].Value == null)
					{
						YJKMessageBox.Show("表格中存在空项！");
						return false;
					}
					string text = dataGridViewRow.Cells[0].Value.ToString();
					string text2 = dataGridViewRow.Cells[1].Value.ToString();
					string text3 = dataGridViewRow.Cells[2].Value.ToString();
					if (text == null || text2 == null || text3 == null)
					{
						YJKMessageBox.Show("表格中存在空项！");
						return false;
					}
				}
			}
			return true;
		}

		private void InitGridViewData()
		{
			new List<RelationData>();
			foreach (RelationData relationData in DlgSetRelationMap.ReadXml())
			{
				int index = this.dataGridView.Rows.Add();
				this.dataGridView.Rows[index].Cells[0].Value = relationData.strPosition;
				this.dataGridView.Rows[index].Cells[1].Value = relationData.strKey;
				this.dataGridView.Rows[index].Cells[2].Value = relationData.strDirection;
			}
		}

		public static List<RelationData> ReadXml()
		{
			List<RelationData> list = new List<RelationData>();
			string text = Path.GetTempPath() + "DlgSetRelationMap.xml";
			if (!File.Exists(text))
			{
				text = AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\DlgSetRelationMapDefult.xml";
				if (!File.Exists(text))
				{
					return list;
				}
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("RelationDatas/RowData");
			for (int i = 0; i < xmlNodeList.Count; i++)
			{
				list.Add(new RelationData
				{
					strPosition = xmlNodeList[i].Attributes[0].Value,
					strKey = xmlNodeList[i].Attributes[1].Value,
					strDirection = xmlNodeList[i].Attributes[2].Value
				});
			}
			return list;
		}

		private void SaveXml()
		{
			string filename = Path.GetTempPath() + "DlgSetRelationMap.xml";
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode xmlNode = xmlDocument.CreateElement("RelationDatas");
			xmlDocument.AppendChild(xmlNode);
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (!dataGridViewRow.IsNewRow)
				{
					XmlElement xmlElement = xmlDocument.CreateElement("RowData");
					xmlElement.SetAttribute(DlgSetRelationMap.strPos, dataGridViewRow.Cells[0].Value.ToString());
					xmlElement.SetAttribute(DlgSetRelationMap.strKey, dataGridViewRow.Cells[1].Value.ToString());
					xmlElement.SetAttribute(DlgSetRelationMap.strDir, dataGridViewRow.Cells[2].Value.ToString());
					xmlNode.AppendChild(xmlElement);
				}
			}
			xmlDocument.Save(filename);
		}

		private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			DataGridView dataGridView = sender as DataGridView;
			if (dataGridView.CurrentCell.ColumnIndex == 0 && dataGridView.CurrentCell.RowIndex != -1)
			{
				(e.Control as ComboBox).SelectedIndexChanged += this.ComboBox_SelectedIndexChanged;
			}
		}

		private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			ComboBox comboBox = sender as ComboBox;
			comboBox.Leave += this.combox_Leave;
			try
			{
				if (comboBox.SelectedItem != null)
				{
					int rowIndex = this.dataGridView.CurrentCell.RowIndex;
					string text = comboBox.Text;
					if (text == "墙壁明装" || text == "墙壁暗装")
					{
						this.dataGridView.Rows[rowIndex].Cells[2].Value = "中";
					}
					else if (text == "地面安装")
					{
						this.dataGridView.Rows[rowIndex].Cells[2].Value = "下";
					}
					else
					{
						this.dataGridView.Rows[rowIndex].Cells[2].Value = "上";
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public void combox_Leave(object sender, EventArgs e)
		{
			(sender as ComboBox).SelectedIndexChanged -= this.ComboBox_SelectedIndexChanged;
		}

		private static string strPos = "设备安装位置";

		private static string strKey = "对应关键字";

		private static string strDir = "设备安装方向";
	}
}
